<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function LinkedList() {
            function Node(data) {
                this.data = data;
                this.next = null
            }
            this.head = null;
            this.length = 0;
            // 向链表尾部添加一个新的项；
            LinkedList.prototype.append = function (data) {
                let node = new Node(data)
                if (this.length === 0) {
                    this.head = node;

                } else {
                    let current = this.head;
                    while (current.next) {
                        current = current.next;
                    }
                    current.next = node;
                }
                this.length++;
            }
            LinkedList.prototype.toString = function () {
                var str = '';
                var current = this.head;
                while (current) {
                    str += current.data + ' ';
                    current = current.next;
                }
                return str;
            }
            // 向链表的特定位置插入一个新的项；
            LinkedList.prototype.insert = function (position, data) {
                let newNode = new Node(data)
                if (position < 0 || position > this.length) return;
                if (position == 0) {
                    newNode.next = this.head;//先让新创建的节点指向头节点
                    this.head = newNode;//再让头节点前移
                } else {
                    var current = this.head;
                    for (let i = 0; i < position - 1; i++) {
                        current = current.next;
                    }
                    newNode.next = current.next;
                    current.next = newNode
                }
                this.length++;
            }
            // get（position）：获取对应位置的元素；
            LinkedList.prototype.get = function (position) {
                if (position < 0 || position > this.length) return null;
                var current = this.head;
                for (let i = 0; i < position; i++) {
                    current = current.next;
                }
                return current.data;
            }
            // indexOf（element）：返回元素在链表中的索引。如果链表中没有该元素就返回-1；
            LinkedList.prototype.indexOf = function (data) {
                var current = this.head;
                var index = 0;
                while (current) {
                    if (current.data === data) {
                        return index;
                    }
                    current = current.next;
                    index++;
                }
                return -1;
            }
            // update（position，element）：修改某个位置的元素；
            LinkedList.prototype.update = function (position, data) {
                if (position < 0 || position > this.length) return;
                var current = this.head;
                for (let i = 0; i < position; i++) {
                    current = current.next;
                }
                current.data = data;
            }
            // removeAt（position）：从链表的特定位置移除一项；
            LinkedList.prototype.removeAt = function (position) {
                if (position < 0 || position >= this.length) return;
                if (position == 0) {
                    this.head = this.head.next;
                } else {
                    var current = this.head;
                    for (let i = 0; i < position - 1; i++) {
                        current = current.next;
                    }
                    current.next = current.next.next;
                }
                this.length--;

            }
            // remove（element）：从链表中移除一项；
            LinkedList.prototype.remove = function (data) {

                var index = this.indexOf(data)
                this.removeAt(index)

                // 调用removeAt()已经减一不必再减
                // this.length--;
            }
            // isEmpty（）：如果链表中不包含任何元素，返回trun，如果链表长度大于0则返回false；
            LinkedList.prototype.isEmpty = function () {
                return this.length > 0 ? false : true;
            }
            // size（）：返回链表包含的元素个数，与数组的length属性类似；
            LinkedList.prototype.size = function () {
                return this.length;
            }
        }

        var linkedList = new LinkedList()
        linkedList.append('a');
        linkedList.append('b');
        linkedList.append('c');
        linkedList.insert(3, 'd');
        // linkedList.insert(1, 'e');
        // linkedList.remove('e');
        console.log(linkedList.size());
        console.log(linkedList.toString());
    </script>
</body>

</html>